package com.xc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.common.Const;
import com.xc.common.ServerResponse;
import com.xc.dao.SiteBannerMapper;
import com.xc.pojo.SiteBanner;
import com.xc.service.ISiteBannerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/3/17 11:26 AM
 */
@Service("iSiteBannerService")
@Slf4j
public class SiteBannerServiceImpl implements ISiteBannerService {

    @Autowired
    SiteBannerMapper siteBannerMapper;

    @Override
    public ServerResponse add(SiteBanner siteBanner) {
        if(StringUtils.isBlank(siteBanner.getBannerUrl())
                || siteBanner.getIsOrder() == null
                || siteBanner.getIsPc() == null
                || siteBanner.getIsM() == null
                ){
            return ServerResponse.createByErrorMsg("参数不能为空");
        }

        siteBanner.setAddTime(new Date());
        int insertCount = siteBannerMapper.insert(siteBanner);
        if(insertCount > 0){
            return ServerResponse.createBySuccessMsg("添加成功");
        }else{
            return ServerResponse.createByErrorMsg("添加失败");
        }
    }

    @Override
    public ServerResponse listByAdmin(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SiteBanner> siteBanners = siteBannerMapper.listByAdmin();
//        log.info("siteBanners  = {}" , siteBanners.size());
        PageInfo pageInfo = new PageInfo(siteBanners);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse update(SiteBanner siteBanner) {
        if(siteBanner == null){
            return ServerResponse.createByErrorMsg("id不能为空");
        }

        int updateCount = siteBannerMapper.updateByPrimaryKeySelective(siteBanner);
        if(updateCount > 0){
            return ServerResponse.createBySuccessMsg("修改成功");
        }else{
            return ServerResponse.createByErrorMsg("修改失败");
        }
    }

    @Override
    public ServerResponse delete(Integer id) {
        if(id == null){
            return ServerResponse.createByErrorMsg("id不能为空");
        }
        int deleteCount = siteBannerMapper.deleteByPrimaryKey(id);
        if(deleteCount > 0){
            return ServerResponse.createBySuccessMsg("删除成功");
        }else{
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }

    @Override
    public ServerResponse getBannerByPlat(String platType) {

        if(Const.SiteBannerPlatType.M_TYPE.equals(platType)){
            List bannerList = siteBannerMapper.getBannerByMobile();
            return ServerResponse.createBySuccess(bannerList);
        }else if(Const.SiteBannerPlatType.PC_TYPE.equals(platType)){
            List bannerList = siteBannerMapper.getBannerByPC();
            return ServerResponse.createBySuccess(bannerList);
        }else{
            return ServerResponse.createByErrorMsg("不存在的平台类型");
        }
    }


}
